package com.enhe.gck.service.impl;

import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.enums.WeighingResultEnum;
import com.enhe.gck.exception.BusinessException;
import com.enhe.gck.exception.WeighException;
import com.enhe.gck.pojo.LoadingBatchRecord;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.panel.CarbonPanel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BatchRecordService {

    public void saveBatch(List<LoadingBatchRecord> batchRecordList) {
        Collection<IntegrationMsg> msgCollection = CarbonPanel.getRecordIntegration(CarbonBaseConstant.装车批次记录_zcpcjl)
                .integrate(batchRecordList);
        if (msgCollection.stream().anyMatch(msg -> !msg.success())) {
            String errMsg = msgCollection.stream().filter(msg -> !msg.success()).map(IntegrationMsg::getRefuseMsgContentStr).collect(Collectors.joining());
            log.error("批量保存装车批次记录失败!原因:{}", errMsg);
            throw new WeighException(WeighingResultEnum.System_Abnormal, errMsg);
        }else {
            log.warn("批量保存装车批次记录成功!");
        }
    }

}
